package com.bsy.videostation.ui.activity

import android.os.Bundle
import android.view.KeyEvent
import android.view.LayoutInflater
import android.widget.ImageView
import androidx.viewpager2.widget.MarginPageTransformer
import com.bsy.framework.adapter.ViewPage2FragmentAdapter
import com.bsy.framework.base.BaseMvvmActivity
import com.bsy.framework.helper.AppHelper
import com.bsy.framework.toast.Toaster
import com.bsy.framework.utils.AppExit
import com.bsy.videostation.view_type.CategoryViewType
import com.bsy.videostation.view_type.HomeViewType
import com.bsy.videostation.viewmodel.MainViewModel
import com.dg.videostation.R
import com.dg.videostation.databinding.ActivityMainBinding
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.gyf.immersionbar.ImmersionBar

class MainActivity : BaseMvvmActivity<ActivityMainBinding, MainViewModel>() {
    private val homePagerAdapter by lazy {
        ViewPage2FragmentAdapter(supportFragmentManager, lifecycle, mViewModel.fragments)
    }
    private var tabMediator: TabLayoutMediator? = null

    //获取tabLayout所有可以点击的item
    private val touchableList by lazy { mBinding.tab.touchables }
    override fun initViewModel() {
        mViewModel = AppHelper.getApplicationViewModel(MainViewModel::class.java)
    }

    override fun initView(savedInstanceState: Bundle?) {
        ImmersionBar.with(this).fullScreen(true).init()
        // 检查tabIcons数组和fragments列表长度是否一致
        if (mViewModel.tabIcons.size != mViewModel.fragments.size) {
            throw IllegalStateException("tabIcons数组和fragments列表长度不一致")
        }
        mBinding.tab.tabMode = TabLayout.MODE_SCROLLABLE
        mBinding.viewpager.isUserInputEnabled = true
        mBinding.viewpager.offscreenPageLimit = mViewModel.fragments.size
        mBinding.viewpager.setPageTransformer(MarginPageTransformer(0))
        mBinding.viewpager.adapter = homePagerAdapter
        if (tabMediator == null) {
            tabMediator =
                TabLayoutMediator(mBinding.tab, mBinding.viewpager, true, true) { tab, index ->
                    val tabItemView =
                        LayoutInflater.from(this)
                            .inflate(R.layout.view_tab_item, mBinding.main, false)
                    val tabIcon = tabItemView.findViewById<ImageView>(R.id.tabIcon)
                    tabIcon.setImageResource(mViewModel.tabIcons[index])
                    tab.customView = tabItemView
                }
            tabMediator?.attach()
        }
    }

    override fun initObserver() {
        mViewModel.historyEvent.observe(this) {
            mBinding.viewpager.isUserInputEnabled = it == HomeViewType.HOME_PAGE.value
        }
        mViewModel.categoryEvent.observe(this) {
            mBinding.viewpager.isUserInputEnabled = it == CategoryViewType.HOME_PAGE.value
        }
        mViewModel.enableTabEvent.observe(this) { enable ->
            touchableList?.forEach { it.isEnabled = enable }
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            AppExit.onBackPressed(this,
                { Toaster.show(getString(com.bsy.framework.R.string.base_app_exit_one_more_press)) })
            return true
        }
        return super.onKeyDown(keyCode, event)
    }

    override fun onDestroy() {
        tabMediator?.detach()
        super.onDestroy()
    }
}